import { createApp } from 'vue'
import { createPinia } from 'pinia'
import { Quasar, Notify } from 'quasar'

// Import Quasar css
import 'quasar/dist/quasar.css'
import '@quasar/extras/material-icons/material-icons.css'

// Import Quasar components
import {
  QPage,
  QBtn,
  QBtnGroup,
  QIcon,
  QFile,
  QLinearProgress,
  QCard,
  QCardSection,
  QList,
  QItem,
  QItemSection,
  QItemLabel,
  QBadge,
  QScrollArea,
  QSpinnerCube,
  QSpace,
  QChip,
  QInput,
  QTooltip,
  QSelect,
  Ripple
} from 'quasar'

import App from './App.vue'
import router from './router'

import './assets/main.css'

const app = createApp(App)

// 注册 Quasar 组件
app.component('QPage', QPage)
app.component('QBtn', QBtn)
app.component('QBtnGroup', QBtnGroup)
app.component('QIcon', QIcon)
app.component('QFile', QFile)
app.component('QLinearProgress', QLinearProgress)
app.component('QCard', QCard)
app.component('QCardSection', QCardSection)
app.component('QList', QList)
app.component('QItem', QItem)
app.component('QItemSection', QItemSection)
app.component('QItemLabel', QItemLabel)
app.component('QBadge', QBadge)
app.component('QScrollArea', QScrollArea)
app.component('QSpinnerCube', QSpinnerCube)
app.component('QSpace', QSpace)
app.component('QChip', QChip)
app.component('QInput', QInput)
app.component('QTooltip', QTooltip)
app.component('QSelect', QSelect)

// 注册 Quasar 指令
app.directive('ripple', Ripple)

// 全局错误处理
app.config.errorHandler = (err, vm, info) => {
  console.error('Vue Error:', err)
  console.error('Error Info:', info)
  console.error('Component:', vm)
}

// 全局警告处理
app.config.warnHandler = (msg, _vm, trace) => {
  console.warn('Vue Warning:', msg)
  console.warn('Trace:', trace)
}

// 捕获未处理的Promise错误
window.addEventListener('unhandledrejection', event => {
  console.error('Unhandled Promise Rejection:', event.reason)
  // 阻止默认的控制台错误信息
  event.preventDefault()
})

// 捕获全局错误
window.addEventListener('error', event => {
  console.error('Global Error:', event.error)
})

app.use(createPinia())
app.use(Quasar, {
  plugins: {
    Notify
  }
})
app.use(router)

// 等待路由准备完成后再挂载
router.isReady().then(() => {
  app.mount('#app')
}).catch(err => {
  console.error('Router initialization failed:', err)
  // 即使路由失败也尝试挂载应用
  app.mount('#app')
})
